package com.hsj.goldstore.mvp.present;

import android.os.Handler;
import android.os.Message;
import android.text.TextUtils;

import com.hsj.goldstore.base.BaseMvpPresent;
import com.hsj.goldstore.base.ResultCallback;
import com.hsj.goldstore.mvp.model.RegistModel;
import com.hsj.goldstore.mvp.view.RegistView;
import com.hsj.goldstore.mvp.view.RegisterActivity;
import com.hsj.goldstore.utils.Helper;

/**
 * Created by Administrator on 2018/3/25.
 */

public class RegisterPresent extends BaseMvpPresent<RegistView> implements IRegisterPresent {

    private int currentTime = 59;
    private boolean stop = false;
    public static final int TIME_MOVE = 0X5000;
    private RegistModel model;

    public RegisterPresent() {
        model = new RegistModel();
    }

    public Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            if (null == mvpView) {
                return;
            }
            if (msg.what == TIME_MOVE) {
                if (stop) {
                    currentTime = 59;
                    stop = false;
                } else if (currentTime > 0) {
                    mvpView.setVerificationText("重新获取(" + currentTime + "s)");
                    currentTime--;
                    handler.sendEmptyMessageDelayed(TIME_MOVE, 1000);
                } else {
                    currentTime = 59;
                    mvpView.setVerificationText("获取验证码");
                    mvpView.setVerificationCodeEnable(true);
                }
            }

        }
    };

    @Override
    public void init() {
        checkViewAttach();
    }

    @Override
    public void sendVerificationCode() {
        handler.sendEmptyMessageDelayed(TIME_MOVE, 1000);
        mvpView.setVerificationCodeEnable(false);
    }

    @Override
    public void register() {
        if (checkInputEmpty()) {
            return;
        }
        if (!mvpView.hasRead()) {
            mvpView.showError("请勾选用户协议");
            return;
        }
        requestRegister();
    }

    @Override
    public void forgetPassword() {
        if (checkInputEmpty()) {
            return;
        }
        requestForgetPassword();
    }

    private void requestForgetPassword() {
        mvpView.showProgress("请稍等...");
        model.forgetPassword(mvpView.getPhone(), mvpView.getVerificatonCode(), mvpView.getPwd(), new ResultCallback() {
            @Override
            public void onSuccess(Object data, int code, String msg) {
                mvpView.hideProgress();
                mvpView.gotoMainActivity();
            }

            @Override
            public void onFalure(int code, String err) {
                mvpView.hideProgress();
            }
        });


    }

    @Override
    public void modifyPassword() {
        if (checkInputEmpty()) {
            return;
        }
        requestModifyPassword();
    }

    private void requestModifyPassword() {
        mvpView.showProgress("请稍等...");
        mvpView.gotoMainActivity();
        mvpView.hideProgress();
    }

    /**
     * 注册接口请求
     */
    private void requestRegister() {
        mvpView.showProgress("请稍等...");
        model.regist(mvpView.getPhone(), mvpView.getVerificatonCode()
                , mvpView.getPwd(), new ResultCallback() {
                    @Override
                    public void onSuccess(Object data, int code, String msg) {
                        mvpView.hideProgress();
                        mvpView.gotoMainActivity();
                    }

                    @Override
                    public void onFalure(int code, String err) {
                        mvpView.hideProgress();
                        mvpView.showError(err);
                    }
                });


    }

    /**
     * 空值等检测
     */
    private boolean checkInputEmpty() {

        if (TextUtils.isEmpty(mvpView.getPhone())) {
            mvpView.showError("请输入手机号");
            return true;
        }
        if (TextUtils.isEmpty(mvpView.getVerificatonCode())) {
            mvpView.showError("请输入验证码");
            return true;
        }
        if (TextUtils.isEmpty(mvpView.getPwd())) {
            mvpView.showError("请输入密码");
            return true;
        }
        if (TextUtils.isEmpty(mvpView.getPwdAgain())) {
            mvpView.showError("请输入密码");
            return true;
        }
        if (mvpView.getPwd().length() < 6 || mvpView.getPwd().length() > 12) {
            mvpView.showError("请输入密码6-12位");
            return true;
        }
        if (!mvpView.getPwd().equals(mvpView.getPwdAgain())) {
            mvpView.showError("两次密码不一致");
            return true;
        }
        return false;

    }
}
